const { error } = require("jquery");
const { success } = require("toastr");

define(['jquery', 'bootstrap', 'fast', 'form','vue'], function ($, undefined, Fast, Form,Vue) {
    var Controller = {
        index: function () {
            const vm =new Vue({
                el: '#app',
                data:{        
                    user:null,//用户信息
                    head:[], //头部导航
                    banner:[],//轮播图
                    searchQuery: '',//搜索框
                    suggestions: [],//搜索建议
                    showSuggestions: false,//是否显示建议列表
                    selectedIndex: -1,//当前选中的建议索引
                    currentCategory:0,
                    itemTab:0,
                    items:[],
                    page:1,
                    list:[],
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        },
                        
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                that.banner = result.data.banner;
                                that.head = result.data.head;
                            }
                        }
                    });        
                    Fast.api.ajax({
                        url: 'ajax/adviser',
                        async:true,
                        loading:false,
                        success:function(result) {
                            if (result.code == 200) {
                                that.items = result.data.data;
                                that.currentCategory = result.data.year;
                                that.itemTab = result.data.month;
                            }
                        }
                    });
                                
                },
                mounted() {
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                    // 点击页面其他地方时关闭建议列表
                    document.addEventListener('click', (e) => {
                        if (!e.target.closest('.search-box')) {
                            that.showSuggestions = false;
                        }
                    });
                   
                   
                },
                methods:{
                    handle(url,isLogin){
                        console.log(url);
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                                
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    showFavorites(id){
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.ajax({
                            url: "member/submit",
                            loading: false,
                            async:true,
                            data:{t:'adv',id:id,c:'fa'},
                            
                        });
                    },
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handCate(item,index){
                        this.currentCategory=index;
                        this.itemTab=item;
                        const element = document.getElementById(index+'-'+item);
                        element.scrollIntoView({ behavior: 'smooth' });
                    },
                    handleShow() {
                        this.getData();                       
                    },
                    handleAdviser(item){
                        console.log(item);
                        Fast.api.ajax({
                            url: '/adviser/guide',
                            data: {
                                id: item,
                            },
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 1) {
                                    window.location.href = '/adviser/guide?id='+item;
                                }else{
                                    Fast.api.msg('无权查看');
                                }
                            },
                            error:function(result) {
                                Fast.api.msg('无权查看');
                            }
                        })
                    },
                    handleSearch() {
                        if (!this.searchQuery) {
                            this.suggestions = [];
                            this.showSuggestions = false;
                            return;
                        } 
                        Fast.api.ajax({
                            url: 'ajax/searchlog',
                            data: {
                                query: this.searchQuery,
                            },
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 200) {
                                    that.suggestions = result.data;
                                    that.showSuggestions = true;
                                    that.selectedIndex = -1;
                                }
                            }
                        });
                        
                    },
                    handleKeyDown(e) {
                        if (!this.suggestions.length) return;
                    
                        switch(e.key) {
                            case 'ArrowDown':
                                e.preventDefault();
                                this.selectedIndex = (this.selectedIndex + 1) % this.suggestions.length;
                                break;
                            case 'ArrowUp':
                                e.preventDefault();
                                this.selectedIndex = this.selectedIndex <= 0 ? this.suggestions.length - 1 : this.selectedIndex - 1;
                                break;
                            case 'Enter':
                                if (this.selectedIndex > -1) {
                                    this.selectSuggestion(this.suggestions[this.selectedIndex]);
                                } else {
                                    this.submitSearch();
                                }
                                break;
                            case 'Escape':
                                this.showSuggestions = false;
                                break;
                        }
                    },
                    selectSuggestion(item) {
                        this.searchQuery = item.name;
                        this.showSuggestions = false;
                        
                    },
                    submitSearch() {
                       window.location.href = '/adviser?keywords='+this.searchQuery;
                    }
                }
            });
            $(document).on("click",'.activity-tabs > .activity-tags > .tags',function(){
                if($(this).hasClass('active')){
                    return;
                }
                var tagId =$(this).data('tabs');
                var cards = $(this).closest('.activity-tabs').find('.activity-card');
            
                cards.each(function() {
                    var cardIds = $(this).data('ids').toString().split(',');
                    if (tagId === 0 || cardIds.includes(tagId.toString())) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                });
                $(this).addClass('active').siblings().removeClass('active');
            })
        },
        convention:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:null,
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                },
                mounted() {
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                },
                methods:{
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },

                    
                }
            });
        },
        guide:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:null,
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                },
                mounted() {
                    
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                    const hash = window.location.hash.slice(1);
                    if (hash) {
                        const element = document.getElementById(hash);
                        if (element) {
                            element.scrollIntoView({ behavior:'smooth' });
                        }
                    }
                },
                methods:{
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    
                }
            });
        },
        tutorial:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:null,
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                },
                mounted() {
                    console.log(this.item);
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                },
                methods:{
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    
                }
            });
        },
        detail:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:null,
                    currentTab:'about',
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                },
                mounted() {
                    const hash = window.location.hash.slice(1);
                    if (hash) {
                        this.currentTab = 'log';
                    }
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                   
                    if (hash) {
                        const element = document.getElementById(hash);
                        if (element) {
                            element.scrollIntoView({ behavior:'smooth' });
                        }
                    }
                },
                methods:{
                    handlebm(id){
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可报名', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.open('/adviser/baoming?id='+id,'活动报名',{
                            maxmin: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    window.location.reload();
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });

                    },
                    handledaka(id){
                        Fast.api.open('/adviser/logoedit?id='+id,'打卡',{
                            maxmin: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });

                    },
                    handlesc(id){
                        Fast.api.open('/adviser/edit?id='+id,'上传手册',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });

                    },
                    handleCurrent(tab) {
                        this.currentTab = tab;
                    },
                    handleAdviser(item){
                        Fast.api.ajax({
                            url: '/adviser/guide',
                            data: {
                                id: item,
                            },
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 1) {
                                    window.location.href = '/adviser/guide?id='+item;
                                }else{
                                    Fast.api.msg('无权查看');
                                }
                            }
                        })
                    },
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    
                }
            });
            $(document).on("click",'.tabtags',function(){
                if($(this).hasClass('activec')){
                    return;
                }
                var tagId =$(this).data('tabs');
                var cards = $('.logItem');
            
                cards.each(function() {
                    var cardIds = $(this).data('ids').toString().split(',');
                    if (tagId === 0 || cardIds.includes(tagId.toString())) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                });
                $(this).addClass('activec').siblings().removeClass('activec');
            })
        },
        author:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:null,
                    currentTab:'about',
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                },
                mounted() {
   
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                },
                methods:{
                    
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    
                }
            });
        },
        lists:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:null,
                    page:1,
                    list:[],
                    more:true,
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                    this.getData();
                },
                mounted() { 
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                    window.addEventListener('scroll', this.handleScroll);
                },
                beforeDestroy() {
                  // 页面销毁前移除滚动事件监听
                  window.removeEventListener('scroll', this.handleScroll);
                },
                methods:{
                    handleScroll() {
                        const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
                        const windowHeight = window.innerHeight || document.documentElement.clientHeight;
                        const documentHeight = document.documentElement.scrollHeight;
                        if (scrollTop + windowHeight >= (documentHeight-30)) {
                            that.getData();
                        }
                    },
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handleFollow(id){
                        Fast.api.ajax({
                            url: "member/submit",
                            loading: false,
                            async:true,
                            data:{t:'adviser',id:id,c:'adv'},
                            success:function(data) 
                            {
                                if (data.code == 1) {
                                    Fast.api.toastr.success(data.msg);
                                }else{
                                    Fast.api.toastr.error(data.msg);
                                }
                            },
                            error:function(data) {
                                Fast.api.toastr.error(data.msg);
                            }
                        });
                    },
                    getData(){
                        let that = this;
                        if(that.more == false){
                            return;
                        }
                        if(that.page > 1){
                            if(!this.user){
                                window.location.href = '/login';
                                return;
                            }
                            if( this.user.level == 0){
                                Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                    Fast.api.close(index);
                                    that.handleBuy();
                                });
                                return ;
                            }
                        }
                        // 在这里处理日期变化后的逻辑
                        const form = {
                            page:this.page,
                            adviser_id:aid
                        }
                        let page = this.page;
                        Fast.api.ajax({
                            url: 'adviser/lists',
                            data: form,
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 200) {
                                   if(that.page == 1){
                                    that.list = result.data;
                                   }else{
                                    that.list.push(...result.data);
                                   }
                                   if(result.data.length <10){
                                    that.more = false;
                                   }
                                   that.page = page+1;
                                }
                                
                            }
                        });
                    },
                }
            });
        },
        logedit:function(){
            Form.api.bindevent($('form[role=form]'));
        },
        edit:function(){
            Form.api.bindevent($('form[role=form]'));
        },
        baoming:function(){
            const vm =new Vue({
                el: '#app',
                data:{
                    selectTabble:true,               
                    user:{
                        'wx_code':'',
                        'mobile':''
                    },//用户信息
                    levelTab:0,//当前选中的tab
                    payTab:'alipay',
                    list:[],
                    timer:null,
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    this.getData();
                    
                },
                mounted() {
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                },
                methods:{
                    handleBuyLevel(){
                        let that = this;
                        const form = {
                            level:this.levelTab,
                            pay:this.payTab,
                        }
                        Fast.api.ajax({
                            url: "member/level",
                            loading: false,
                            async:true,
                            data:form,
                            success:function(result) {
                                if (result.code == 1) {
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handleCurrent(tab) {
                        this.levelTab = tab;
                    },
                    handleTab(tab){
                        this.payTab = tab;
                    },
                    handleBuyBM(){
                        const form = {
                            'wx_code':this.user.wx_code,
                            'mobile':this.user.mobile,
                            'type':this.payTab,
                            'id':aid,
                        }
                        Fast.api.ajax({
                            url: 'adviser/baoming',
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 1) {
                                    Fast.api.layer.open({
                                        type: 1, 
                                        title: result.data.order.data,
                                        content: `<div style="padding: 15px;diplay:flex;flex-direction:column;align-items: center;justify-content:center;">
                                            <img src="`+result.data.url+`" width="320px" height="320px">
                                            <div style="font-size:18px;font-weight:600;padding:10px;">请使用`+(result.data.order.type=='wechat'?`微信`:`支付宝`)+`扫码支付</div>
                                            <div style="font-size:16px;padding:10px;">订单金额:`+result.data.order.price+`</div>
                                            <div style="font-size:16px;padding:10px;">订单5分钟内有效</div>
                                        </div>`
                                      });
                                    that.timer = setTimeout(function(){
                                        that.getReturnData(result.data.order.out_trade_no);
                                    }, 1500);
                                }else{
                                    Fast.api.toastr.error(data.msg);
                                }
                            }
                        })
                    },
                    getReturnData(out_trade_no){
                        let that = this;
                        clearTimeout(that.timer);
                        Fast.api.ajax({
                            url: "/index/payment/return",
                            loading: false,
                            async:true,
                            data:{out_trade_no:out_trade_no},
                            success:function(result) {
                                if (result.code == 1) {
                                        Fast.api.toastr.success('报名成功');
                                        parent.location.reload();
                                }else{
                                    that.timer = setTimeout(function(){
                                        that.getReturnData(out_trade_no);
                                    }, 1500); 
                                }
                            }
                        });
                    },
                    getData(){
                        let that = this;
                        Fast.api.ajax({
                            url: 'ajax/userlevel',
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 200) {
                                    that.list = result.data;
                                    that.levelTab = result.data[0].id;
                                }
                            }
                        });
                    },
                }
            });
        },
    };
    return Controller;
});

